<template>
  <div class="department-box">
    <el-button size="mini" type="success" @click="addFormAlert">添加</el-button>
    <el-table :data="departmentTable" stripe style="width: 100%">
      <el-table-column prop="nid" label="系编号"></el-table-column>
      <el-table-column prop="name" label="系名称"></el-table-column>
      <el-table-column prop="dean" label="系主任"></el-table-column>
      <el-table-column prop="superior.name" label="所属分院"></el-table-column>
      <el-table-column fixed="right" label="操作" width="200">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div :class="[formIsAlert?'':'close','add-form-bg']">
      <el-form
        ref="addForm"
        :model="addForm"
        class="add-form"
        label-width="100px"
        @submit.native.prevent="addDepartment"
      >
        <el-form-item label="系名称">
          <el-input v-model="addForm.name"></el-input>
        </el-form-item>
        <el-form-item label="系主任">
          <el-input v-model="addForm.dean"></el-input>
        </el-form-item>
        <el-form-item label="所属分院">
          <el-select v-model="collegeOpetion.value" placeholder="请选择" @change="cOptionChange">
            <el-option
              v-for="item in collegeOpetion.list"
              :key="item._id"
              :label="item.name"
              :value="item._id"
            ></el-option>
          </el-select>
        </el-form-item>
        <div class="button-item">
          <el-button class="sure-button" type="primary" native-type="submit">确定</el-button>
          <el-button @click="close">取消</el-button>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      isEdit: false,
      collegeOpetion: {
        list: [],
        value: ""
      },
      departmentTable: [],
      formIsAlert: false,
      addForm: {
        name: "",
        dean: "",
        superior: ""
      },
      putId: ""
    };
  },
  methods: {
    async addDepartment() {
      if (!this.addForm.name) {
        this.$message({
          message: "请填写系别名称",
          type: "warning"
        });
        return;
      }
      if (!this.addForm.superior) {
        this.$message({
          message: "请选择所属分院",
          type: "warning"
        });
        return;
      }
      if (!this.isEdit) {
        const res = await this.$http.post("rest/Department", this.addForm);
        this.handleMessage(res.data);
      } else {
        const res = await this.$http.put(
          `rest/Department/${this.putId}`,
          this.addForm
        );
        this.handleMessage(res.data);
      }
      this.close();
      this.getDepartmentList();
    },
    handleMessage(res) {
      if (res.success) {
        this.$message({ type: "success", message: res.message });
      } else if (!res.success) {
        this.$message({ type: "warning", message: res.message });
      }
    },
    handleDelete(index, row) {
      this.$confirm(`是否确定要删除 "${row.name}"`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(async () => {
        const res = await this.$http.delete(`rest/Department/${row._id}`);
        this.handleMessage(res.data);
        this.getDepartmentList();
      });
    },
    addFormAlert() {
      this.isEdit = false;
      this.formIsAlert = true;
    },
    handleEdit(index, row) {
      this.isEdit = true;
      this.addForm = {
        name: row.name,
        dean: row.dean,
        superior: row.superior._id
      };
      this.formIsAlert = true;
      this.putId = row._id;
      this.collegeOpetion.value = row.superior.name;
    },
    close() {
      this.addForm = {
        name: "",
        dean: "",
        superior: ""
      };
      this.collegeOpetion.value = "";
      this.formIsAlert = false;
    },
    async getDepartmentList() {
      const res = await this.$http.get("Department");
      this.departmentTable = res.data;
    },
    async getCollegeList() {
      const res = await this.$http.get("rest/College");
      this.collegeOpetion.list = res.data;
    },
    cOptionChange(e) {
      this.addForm.superior = e;
    }
  },
  created() {
    this.getDepartmentList();
    this.getCollegeList();
  }
};
</script>

<style lang='stylus'>
.department-box {
  .add-form-bg {
    position: absolute;
    top: 0;
    right: 0;
    left: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.4);
    z-index: 99;
  }

  .close {
    display: none;
  }

  .add-form {
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    position: fixed;
    padding: 50px;
    background: #ffffff;
    padding-left: 30px;
    border-radius: 10px;

    .button-item {
      display: flex;
      justify-content: center;
    }

    .sure-button {
      margin-right: 80px;
      margin-left: 30px;
    }
  }
}
</style>